<?php

!defined('IN_FENGTOP') && exit('Access Denied');
 
class control extends base{

	function control(& $get,& $post){
		$this->base( & $get,& $post);
		$this->load('category');
		$this->load("doc");
		$this->load("user");
	}

	function doview(){
		$getPost = "select `pid`, `author`, `tid`, `fid`, `subject`, `message` , `origin` 
		              from `".DB_TABLEPRE."post` 
		             where `pid` = ".$this->get[2];
//echo $getPost;exit;
		$doc      = $this->db->result_first($getPost);
		
		$getReply = "select `author`, `message`
		               from `".DB_TABLEPRE."post` 
		              where `tid` = {$doc['pid']}
		                and `first` = '0'
		           order by `dateline` ASC";
		$reply      = $this->db->get_array($getReply);
//print_r($doc);
//echo $getReply;
//print_r($reply);
		$categoryJson = $_ENV['category']->get_category_by_fid($doc['fid']);
//echo $categoryJson;
		$category     = $_ENV['category']->get_categrory_tree($categoryJson);
		$this->view->assign('category',$category);

		
		$this->view->assign('doc',$doc);
		$this->view->assign('reply',$reply);
		
		$this->view->display('view.tpl');
	}
	
	function docreate(){
		if(isset($this->post['publishsubmit'])){
			$this->autosave();
//print_r($this->post);
			if(trim($this->post['title'])=="" ){
				$this->message($this->view->lang['createDocTip1'],'BACK',0);
			}

			if(trim($this->post['content'])=="" ){
				$this->message($this->view->lang['createDocTip18'],'BACK',0);
			}
			
//echo trim($this->post['code1']).'||';
//print_r($_ENV['user']->get_code());
			$sessionCode = $_ENV['user']->get_code();
			if(strtolower(trim($this->post['code1']))!=$sessionCode['code'] ){
				$this->message($this->view->lang['createDocTip19'],'BACK',0);
			}
//echo 'here';
			$doc['selType'] = $this->post['selType'];
			$doc['title']   = string::substring(string::stripscript($_ENV['doc']->replace_danger_word(trim($this->post['title']))),0,80);
			$doc['content'] = string::stripscript($_ENV['doc']->replace_danger_word($this->post['content']));
			
			$did=$_ENV['doc']->add_doc($doc);
			
			Header("Location:".FTforum_URL.'/index.php?doc-view-'.$did);
		}else{
			
			$this->view->assign('docid',$this->get[2]);
			
			$this->view->display('createdoc.tpl');
		}
//print_r($this->post);
	}
	
	function doreply(){
		if(isset($this->post['reply'])){
			$this->autosave();
//print_r($this->post);
			if(trim($this->post['content'])=="" ){
				$this->message($this->view->lang['createDocTip18'],'BACK',0);
			}
			
//echo trim($this->post['code1']).'||';
//print_r($_ENV['user']->get_code());
			$sessionCode = $_ENV['user']->get_code();
			if(strtolower(trim($this->post['code1']))!=$sessionCode['code'] ){
				$this->message($this->view->lang['createDocTip19'],'BACK',0);
			}
//echo 'here';
			//$doc['selType'] = $this->post['selType'];
			//$doc['title']   = string::substring(string::stripscript($_ENV['doc']->replace_danger_word(trim($this->post['title']))),0,80);
			
			$doc['pid'] = $this->post['pid'];
			$doc['fid'] = $this->post['fid'];
			$doc['tid'] = $this->post['tid'];
			$doc['content'] = string::stripscript($_ENV['doc']->replace_danger_word($this->post['content']));
			
			$did=$_ENV['doc']->add_doc($doc);
			
			Header("Location:".FTforum_URL.'/index.php?doc-view-'.$this->post['pid']);
		}
	}
	
	function autosave(){
		$did=isset($this->get[2])?$this->get[2]:$this->post['did'];
		$id=isset($this->post['id'])?$this->post['id']:-1;
		$notfirst=isset($this->post['notfirst'])?$this->post['notfirst']:0;
		$savecontent=isset($this->post['content'])?$this->post['content']:'';
		if (FTforum_CHARSET == 'GBK'){$savecontent = string::hiconv($savecontent);}
		if($savecontent!==''){
			$_ENV['doc']->update_autosave($this->user['uid'],$did,$savecontent,$id,$notfirst);
		}
		//$this->message('sucess','',2);
	}
	
	function doedit(){
//print_r($this->post);
		if(isset($this->post['publishsubmit'])){
			$this->autosave();

			if(trim($this->post['title'])=="" ){
				$this->message($this->view->lang['createDocTip1'],'BACK',0);
			}

			if(trim($this->post['content'])=="" ){
				$this->message($this->view->lang['createDocTip18'],'BACK',0);
			}
			
			$sessionCode = $_ENV['user']->get_code();
			if(strtolower(trim($this->post['code1']))!=$sessionCode['code'] ){
				$this->message($this->view->lang['createDocTip19'],'BACK',0);
			}

			$doc['selType'] = $this->post['selType'];
			$doc['title']   = string::substring(string::stripscript($_ENV['doc']->replace_danger_word(trim($this->post['title']))),0,80);
			$doc['content'] = string::stripscript($_ENV['doc']->replace_danger_word($this->post['content']));
			
			$did=$_ENV['doc']->update_field($doc, $this->get[2]);
			
			Header("Location:".FTforum_URL.'/index.php?doc-view-'.$this->get[2]);
		}else{
			$getPost = "select `pid`, `author`, `tid`, `fid`, `subject`, `message` , `origin` 
						from `".DB_TABLEPRE."post` 
						where `pid` = ".$this->get[2];
			$doc      = $this->db->result_first($getPost);
			
//print_r($doc);
			$this->view->assign('doc',$doc);
			$this->view->display('editor.tpl');
		}
	}
	
}
?>